package com.example.telephonehome;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.google.gson.Gson;
import java.io.IOException;
import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {
    private Button bt;
    private TextView tx;
    private EditText et;
    Province province=new Province();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bt=findViewById(R.id.bt_1);
        tx=findViewById(R.id.tx_2);
        et=findViewById(R.id.et_1);
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                sendmass();
            }
        });
    }
    public void sendmass(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                String sj=et.getText().toString();
                OkHttpClient okHttpClient=new OkHttpClient();
                Request request=new Request.Builder().get().url("https://apis.juhe.cn/mobile/get?phone="+sj+"&dtype=&key=a3eaf52975c81e6156b4d28abe3e638c").build();
                Call call=okHttpClient.newCall(request);
                try {
                    Response response=call.execute();
                    Gson gson=new Gson();
                    province=gson.fromJson(response.body().string(),Province.class);
                    settext();
                }catch (IOException e){
                    e.printStackTrace();
                }

            }
        }).start();

    }


    private  void settext(){
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                tx.setText("手机号码所属省份为："+province.getResult().getProvince()+",城市为："+province.getResult().getCity()+",运营商为："+province.getResult().getCompany());
            }
        });
    }
}
